﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using System.Globalization;
using TPCN.Common;
using TPCN.Commons;

namespace TPCN
{
    public partial class Default1 : System.Web.UI.Page
    {
        private List<HangObj> source;
        private const string CMD_ADDTOCART = "AddToCart";

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDataToList();
            BindListView();
        }

        protected void LoadDataToList()
        {
            source = new List<HangObj>();
            if (Request.QueryString["CateID"] != null)
            {
                string cateID = Request.QueryString["CateID"].ToString().Trim();
                if (cateID != "" && Convert.ToInt32(cateID) > 0)
                {
                    source = HangDAL.GetByCategoryID(cateID);
                }
                else
                    Response.Redirect("~/Default.aspx");
            }
            else if (Request.QueryString["q"] != null)
            {
                string query = Request.QueryString["q"].ToString().Trim();
                if (query != "")
                {
                    source = HangDAL.GetByQuery(query);
                }
                else
                    Response.Redirect("~/Default.aspx");
            }
            else
            {
                source = HangDAL.GetByAll();
            }
        }

        protected void BindListView()
        {
            HangListView.DataSource = source;
            HangListView.DataBind();
        }

        protected string ProcessSale(string _giathitruong, string _giabanle)
        {
            double giathitruong, giabanle;
            giathitruong = Convert.ToDouble(_giathitruong);
            giabanle = Convert.ToDouble(_giabanle);
            int percent = Convert.ToInt32(100 - (giabanle / giathitruong) * 100);
            return "-" + Convert.ToString(percent) + "%";
        }

        protected string FormatString(object input)
        {
            int result = 0;
            try
            {
                result = Convert.ToInt32(input);
            }
            finally
            {

            }
            return result.ToString("N0", CultureInfo.CurrentCulture);
        }

        protected string BuildProductDetailPath(object obj)
        {
            return "~/Chitietsanpham.aspx?id=" + Convert.ToString(obj);
        }

        protected void HangListView_ItemCommand(object sender, Telerik.Web.UI.RadListViewCommandEventArgs e)
        {
            string HangID = e.CommandArgument.ToString();
            if (e.CommandName == CMD_ADDTOCART)
            {
                ShoppingCartObj cart;
                ShoppingCartItem item = new ShoppingCartItem(HangID);
                if (HttpContext.Current.Session["Cart"] == null)
                {
                    cart = new ShoppingCartObj();
                    HttpContext.Current.Session["cart"] = cart;
                }
                else
                {
                    cart = (ShoppingCartObj)HttpContext.Current.Session["cart"];
                }
                cart.Insert(item);
                Response.Redirect("~/Giohang.aspx");
            }
        }

        protected string cutProductName(object obj)
        {
            return StringHelper.CutString(Convert.ToString(obj), 35);
        }
    }
}